<template>
  <div>
    <div id="table">
      <el-table
        :data="tableData"
        style="width: 100%">
        <el-table-column
          prop="id"
          label="#"
          width="180">
        </el-table-column>
        <el-table-column
          prop="name"
          label="姓名"
          width="180">
        </el-table-column>
        <el-table-column
          prop="age"
          label="年龄"
          width="180">
        </el-table-column>
        <el-table-column
          prop="gender"
          label="性别"
          width="180">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleAdd()">增加
            </el-button>
            <el-button
              size="mini"
              type="success"
              @click="handleEdit(scope.$index, scope.row)">编辑
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script>
  export default {
    name: 'HelloWorld',
    data() {
      return {
        tableData: []
      }
    },
    methods: {
      handleAdd() {
        this.$router.push("/addStudent")
      },
      handleEdit(index, row) {
        this.$router.push({path: '/updateStudent/', query: {id: row.id}});
      },
      handleDelete(index, row) {
        let vueObj = this
        //弹窗
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          //ajax异步删除
          this.axios({
            method: "delete",
            url: "http://192.168.32.146:8000/student/delete/" + row.id
          })
            .then(function (response) {
              // 跳转到学生列表
              if (response.data === 'ok') {
                vueObj.getData()//重新请求数据
                //弹窗提示
                vueObj.$message({
                  type: 'success',
                  message: '删除成功!'
                });
              }
            })

        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      //请求数据
      getData() {
        let vueObj = this
        this.$http.get("http://192.168.32.146:8000/student/all")
          .then(function (response) {
            console.log("正确响应", response)
            console.log(response.data);
            vueObj.tableData = response.data
          }).catch(function (error) {
          console.log("发生了错误")
          console.log(error)
        })
      }
    },
    //页面加载的时候,渲染列表数据
    mounted() {
      this.getData()
    }
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
